<?php
final class Bootstrap
{
	/**
	 * @var Bootstrap
	 */
	private static $_instance = null;
	
	/**
     * @return Bootstrap
     */
	public static function getInstance()
	{
		if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
	}
	
	/**
	 * void
	 */
	private function __construct() {}
	
	/**
	 * @param string $profile	
	 * @return Bootstrap
	 */
    public function run($profile = 'default')
    {
    	/*mb_internal_encoding("UTF-8");
		mb_regex_encoding("UTF-8");
    	date_default_timezone_set('Europe/Moscow');*/
    	//setlocale(LC_TIME,"russian.UTF-8");
    	//setlocale(LC_ALL, 'rus_RUS.65001');
    	
    	// --- configs --- {{
    	$config = Main_Config::getInstance()->getData();
    	
    	//Zend_Registry::set('Zend_Locale', 'ru_RU');
    	//Zend_Registry::getInstance()->locale = 'ru_RU';
    	 
		//Error_Reporting(E_ALL & ~E_NOTICE);
    	
    	Zend_Registry::getInstance()->config = $config;
    	Zend_Registry::getInstance()->tablePrefix = $config['database']['table_prefix'];
    	Zend_Registry::getInstance()->langAbbr = $config['project']['lang_abbr'];
    	Zend_Registry::getInstance()->langName = $config['project']['lang_name'];
    	Zend_Registry::getInstance()->encoding = $config['project']['encoding'];
    	Zend_Registry::getInstance()->dbAdapter = $config['database'][$profile]['adapter'];
    	//Zend_Registry::getInstance()->
    	// --- configs --- }}    	
    	
        if ($config['project']['debug_mode']) {
            $this->_initFirebug();
        }
        
        Zend_Registry::getInstance()->dbProfile = $profile;
        return $this->_initDb($config['database'][$profile]['adapter'], 
                       $config['database'][$profile]['params'])
                    ->_initDirectoryStructure()
                    ->_initView()
                    ->_initLang()
                    ->_initPlugins();
    }

    /**
     * @param string $adapter
     * @param array $params
     * @return Bootstrap
     */
    private function _initDb($adapter, $params)
    {
    	//$dbAdapter = Zend_Db::factory($adapter, $params);
    	$dbAdapter = Main_Db::factory($adapter, $params);
    	//Zend_Registry::getInstance()->dbAdapter = $dbAdapter;
        //Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
        return $this;
    }
    
    /**
     * @return Bootstrap
     */
    private function _initFirebug()
    {
    	if (is_file($file = "FirePHPCore/fb.php")) {
        	include_once $file;
    	}	
        return $this;
    }

    /**
     * @return Bootstrap
     */
    private function _initDirectoryStructure()
    {
        $modules = Main_Module::getModules();
        $modules[] = 'default';       
        
        $directories = array();
        foreach ($modules as $module) {
            $directories += array($module => APPLICATION_PATH . '/modules/'. $module .'/controllers');
        }    
        
        // пути к контроллерам модулей
        Zend_Controller_Front::getInstance()->setControllerDirectory($directories);
        Zend_Controller_Front::getInstance()->setParam('env', APPLICATION_ENVIRONMENT);
        return $this;
    }
    
    /**
     * @return Bootstrap
     */
    private function _initView() 
    {
    	// Директория с шаблонами layouts
        Zend_Layout::startMvc(APPLICATION_PATH . '/views/layouts');       
        
        $view = Zend_Layout::getMvcInstance()->getView();
        $view->doctype('XHTML1_TRANSITIONAL');
        $view->setBasePath(APPLICATION_PATH. '/views/');

		//$view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
            
        return $this;
    }
    
    /**
     * @return Bootstrap
     */
    private function _initLang() 
    {
    	try {
    		$serviceLang = new App_Admin_Service_Lang();
    		Zend_Registry::getInstance()->langId = $serviceLang->getLangId();
    		//echo Zend_Registry::getInstance()->langId; exit;
    	} catch(Exception $e){
    		Zend_Registry::getInstance()->langId = null;
    	}
    	
    	return $this;
    }
    
    /**
     * @return Bootstrap
     */
    private function _initPlugins() 
    {
    	Zend_Controller_Front::getInstance()->registerPlugin(new Main_Controller_Plugin_Router());
    	
    	return $this;
    }
}
